<?php
/**
 * regd by PhpStorm.
 * User: lee
 */

namespace Biz\validate\trade;

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\service\trade\OrderRefund as sOrderRefund;
use Biz\model\trade\Order as mOrder;
use Biz\model\trade\ShopAddress as mShopAddress;

class OrderRefund extends \Doraemon\pockets\validate\Base
{


    public static function rules()
    {
        $typeRefundList = array_keys(mOrderRefund::$typeRefundDesc);


        $reasonList = array_keys(mOrderRefund::getAllRefundReasonList());

        $judgeResultList = array_keys(mOrderRefund::$rightsJudgeResultDesc);

        $processMethodList = array_keys(mOrderRefund::$rightsProcessMethodDesc);

        return [
            ['reason', 'required', null, '请选择退款原因', ['create','update']],
            ['reason', 'InList', ['list'=>$reasonList], '请正确选择退款原因', ['create','update']],

            ['type_refund','required',null,'请选择退款类型',['create','update']],
            ['type_refund', 'InList', ['list'=>$typeRefundList], '请正确选择退款类型', ['create','update']],
            ['receipt_status', 'callback', ['callback' => [new self(), 'checkReceiptStatus']], '请正确选择收货状态', ['create','update']],


            ['refund_remark', 'length', '0,500', '请正确填写退款说明', ['create','update']],

            //拒绝退款用到的
            ['reject_remark','required',null,'请填写拒绝理由',['reject']],
            ['reject_remark','length','1,500','请正确填写拒绝理由',['reject']],


            //填写物流信息用到的验证器
            ['express_company','required',null,'请选择物流公司',['fillLogistics']],
            ['express_no','required',null,'请填写物流单号',['fillLogistics']],
            ['express_no','length','1,30','请正确填写物流单号',['fillLogistics']],
            ['logistics_remark', 'length', '0,500', '请正确填写附加说明', ['fillLogistics']],

            //发起维权的验证器
            ['rights_remark','length','0,500','请正确填写凭证内容',['launchRights']],
            ['rights_remark', 'callback', ['callback' => [new self(), 'checkLauchRights']], '文字和图片至少填写1项', ['launchRights']],
            ['images_content', 'callback', ['callback' => [new self(), 'checkLauchRights']], '文字和图片至少填写1项', ['launchRights']],


            //提交凭证的验证器
            ['proof_remark','length','0,500','请正确填写维权内容',['fillRightsProof','']],
            ['proof_remark', 'callback', ['callback' => [new self(), 'checkFillRightsProof']], '文字和图片至少填写1项', ['fillRightsProof']],
            ['images_content', 'callback', ['callback' => [new self(), 'checkFillRightsProof']], '文字和图片至少填写1项', ['fillRightsProof']],

            ['images_content', 'callback', ['callback' => [new self(), 'checkImagesContent']], '图片最多支持5张图', ['create','update','reject','fillLogistics','launchRights','fillRightsProof','rightsJudge']],


            //维权判定的验证器
            ['judge_result','required',null,'请选择判定结果',['rightsJudge']],
            ['judge_result','InList',['list'=>$judgeResultList],'请正确选择判定结果',['rightsJudge']],
            ['process_method','required',null,'请选择处理方式',['rightsJudge']],
            ['process_method','InList',['list'=>$processMethodList],'请正确选择处理方式',['rightsJudge']],
            ['judge_remark','required',null,'请填写判定结果说明',['rightsJudge']],
            ['judge_remark','length','1,500','请正确填写判定结果说明',['rightsJudge']],

        ];

    }

    public function checkReceiptStatus($value, $valueIdentifier, $allData){

        switch ($allData['order_status']){
            case mOrder::STATUS_WAIT_SEND:
                if($value != mOrderRefund::RECEIPT_STATUS_NORMAL){
                    return false;
                }
                break;
            case mOrder::STATUS_SEND_OUT:
                if($value == mOrderRefund::RECEIPT_STATUS_NORMAL){
                    return false;
                }
            case mOrder::STATUS_SUCCESS:
                if(!in_array($value,array_keys(mOrderRefund::$receiptStatusDesc))){
                    return false;
                }
                break;
        }

        return true;
    }

    public function checkImagesContent($value, $valueIdentifier, $allData)
    {
        if(!empty($value)) {
            $value = json_decode($value,true);

            $value = array_filter($value,function($val){
                if (!preg_match('/^[a-zA-Z0-9]+$/', $val)) {
                    return false;
                }

                return true;
            });

            //图片最多支持8张图
            if (count($value) > 5)
                return false;
        }

        return true;
    }



    public function checkLauchRights($value, $valueIdentifier, $allData){
        if(empty($allData['rights_remark']) && empty($allData['images_content'])){
            return false;
        }

        return true;
    }

    public function checkFillRightsProof($value, $valueIdentifier, $allData){
        if(empty($allData['proof_remark']) && empty($allData['images_content'])){
            return false;
        }

        return true;
    }
}